//
//  playMusicTool.m
//  MyMusic
//
//  Created by ioser on 17/5/3.
//  Copyright © 2017年 ioser. All rights reserved.
//

#import "playMusicTool.h"


@implementation playMusicTool

static NSMutableDictionary *_playerDic;

+ (void)initialize
{
    _playerDic = [NSMutableDictionary dictionary];
}

+ (AVAudioPlayer *)playMusicWithName:(NSString *)name
{
    AVAudioPlayer *player = nil;
    player = _playerDic[name];
    if (player == nil) {
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:name withExtension:nil];
        if (fileURL == nil) return nil;
        
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
        
        [_playerDic setValue:player forKey:name];
    }
    
    [player prepareToPlay];
    [player play];
    
    return player;
}

+ (void)stopMusic:(NSString *)name
{
    AVAudioPlayer *player = _playerDic[name];
    if (player) {
        [player stop];
        [_playerDic removeObjectForKey:name];
        player = nil;
    }
}

@end
